package android.Spider;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.view.Menu;
import android.widget.Toast;


public class PrivateMessagesList extends PictureConnectionListActivity
{
	void InitConnection()
	{
		m_connection = null;
		m_timer.schedule(
				new TimerTask() {
					@Override
					public void run()
					{
						final int nSelected = getSelectedItemPosition();
						if (nSelected == m_nCurSel && m_nCurSel >= 0)
						{
							PrivateMessages pm = PrivateMessages.Instance(PrivateMessagesList.this);
							PrivateMessages.Message msg = pm.GetMessageByPos(pm.GetMessagesCount() - 1 - m_nCurSel);
							if (!msg.m_bRead)
							{
								msg.m_bRead = true;
								pm.Store();
								runOnUIThread(new Runnable() { 
									public void run() { ResetItem(m_nCurSel); }  }
								);
								
								ResetItem(nSelected);
							}
						}
						else
						{
							m_nCurSel = nSelected;
						}
					}
				},
				0, 2000);
	}

	@Override
	protected void onDestroy() {
		m_timer.cancel();
		m_timer = null;
		super.onDestroy();
	}

	void ResetItem(int nPos)
	{
		PrivateMessages pm = PrivateMessages.Instance(PrivateMessagesList.this);
		PrivateMessages.Message msg = pm.GetMessageByPos(pm.GetMessagesCount() - 1 - nPos);
		ListItem item = new ListItem();
		item.m_bmpPicture = msg.m_bRead ? 
				getResources().getDrawable(R.drawable.enveloperead) :
				getResources().getDrawable(R.drawable.envelopeunread);
		
		item.m_strPictureDesc = msg.m_bRead ? "Read" : "Unread";
		item.m_strCaption = "From: " + msg.m_strFrom;
		item.m_strFineDesc = msg.m_strMsg;
		
		ResetItem(nPos, item);
		
		if (s_brMother != null)
			s_brMother.UpdateReceivedMessage();
	}
	
	void InitListAdapter() 
	{
		PrivateMessages pm = PrivateMessages.Instance(this);
		ArrayList<PrivateMessages.Message> lstMessages = pm.GetAllMessages();
		ArrayList<ListItem> lstItems = new ArrayList<ListItem>();
		for (int i = lstMessages.size() - 1; i >= 0; --i)
		{
			PrivateMessages.Message msg = lstMessages.get(i);
			ListItem item = new ListItem();
			item.m_bmpPicture = msg.m_bRead ? 
					getResources().getDrawable(R.drawable.enveloperead) :
					getResources().getDrawable(R.drawable.envelopeunread);
			
			item.m_strPictureDesc = msg.m_bRead ? "Read" : "Unread";
			item.m_strCaption = "From: " + msg.m_strFrom;
			item.m_strFineDesc = msg.m_strMsg;
			lstItems.add(item);
		}
		
		SetListItems(lstItems);
		
		if (lstItems.size() == 0)
			Toast.makeText(this, "There are no private messages", Toast.LENGTH_SHORT).show();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    super.onCreateOptionsMenu(menu);
	    
	    menu.add(0, 0, "Send Private Message", R.drawable.privatemsg);
	    menu.add(0, 1, "Reply", R.drawable.reply);
	    menu.add(1, 2, "Delete", R.drawable.delete);
	    menu.add(1, 3, "Delete All", R.drawable.deleteall);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item)
	{
		PrivateMessages pm = PrivateMessages.Instance(PrivateMessagesList.this);
    	int nPos = getSelectedItemPosition();

    	switch(item.getId())
		{
		case 0:
			CreateMessage(null);
			return true;
		case 1:
			if (nPos >= 0)
			{
				CreateMessage(pm.GetMessageByPos(pm.GetMessagesCount() - 1 - nPos).m_strFrom);
				return true;
			}
			break;
		case 2:
	    	if (nPos >= 0)
	    	{
				pm.DeleteMessageByPos(pm.GetMessagesCount() - 1 - nPos);
				pm.Store();
				InitListAdapter();
		    	return true;
	    	}
			break;
		case 3:
	    	pm.Clear();
	    	pm.Store();
	    	InitListAdapter();
	    	return true;
		}
	    
	    return false;
	}

	void CreateMessage(String strTo)
	{
    	Intent intent = new Intent(this, PrivateMessageSend.class);
		intent.putExtra(ThreadDisplay.s_strUser, strTo);
		startSubActivity(intent, 0);
	}
	
	private Timer m_timer = new Timer();
	private int m_nCurSel = -1;
	static Broodmother s_brMother = null;
}
